package br.com.californiahotel.client.viewControler.hospede;

import br.com.californiahotel.client.architecture.viewController.BasicView;
import br.com.californiahotel.client.viewControler.hospede.HospedeFormController.Display;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.SpacerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;

public class HospedeFormView extends BasicView implements Display {

	public enum Field{
		NAME("Nome"),IDENTITY("identidade"),CPF("cpf"),PHONE("telefone"),ADDRESS("endereco"),SAVE("save"),CANCEL("cancel");

		private final String fieldName;

		private Field(String name) {
			fieldName = name;
		}

		public String getFieldName() {
			return fieldName;
		}

	}

	private DynamicForm form;
	private String id;

	public HospedeFormView() {
		mountForm();
		setAutoWidth();
		setHeight100();
		setMargin(20);
	}

	public HospedeFormView(String identidade, String nome,
			String cpf, String telefone, String endereco) {
		this.id = identidade;
		mountForm();
		setAutoWidth();
		setHeight100();
		setMargin(20);
		form.getItem(Field.IDENTITY.getFieldName()).setValue(identidade);
		form.getItem(Field.NAME.getFieldName()).setValue(nome);
		form.getItem(Field.CPF.getFieldName()).setValue(cpf);
		form.getItem(Field.PHONE.getFieldName()).setValue(telefone);
		form.getItem(Field.ADDRESS.getFieldName()).setValue(endereco);
	}

	private void mountForm() {
		form = new DynamicForm();
		form.setNumCols(10);
		form.setShowEdges(true);

		TextItem textItem = new TextItem(Field.NAME.getFieldName(), "Nome");
		textItem.setEndRow(true);
		textItem.setColSpan(10);

		TextItem textItem2 = new TextItem(Field.IDENTITY.getFieldName(), "Identidade");
		textItem2.setEndRow(true);
		textItem2.setColSpan(10);

		TextItem textItem3 = new TextItem(Field.CPF.getFieldName(), "CPF");
		textItem3.setEndRow(true);
		textItem3.setColSpan(10);

		TextItem textItem4 = new TextItem(Field.PHONE.getFieldName(), "Telefone");
		textItem4.setEndRow(true);
		textItem4.setColSpan(10);

		TextItem textItem5 = new TextItem(Field.ADDRESS.getFieldName(), "Endere\u00E7o");
		textItem5.setEndRow(true);
		textItem5.setColSpan(10);

		ButtonItem cancelButtonItem = new ButtonItem(Field.CANCEL.getFieldName(), "Cancelar");
		cancelButtonItem.setAlign(Alignment.RIGHT);
		cancelButtonItem.setAutoFit(true);
		cancelButtonItem.setStartRow(false);
		cancelButtonItem.setEndRow(false);

		ButtonItem saveButtonItem = new ButtonItem(Field.SAVE.getFieldName(), "Salvar");
		saveButtonItem.setAlign(Alignment.RIGHT);
		saveButtonItem.setAutoFit(true);
		saveButtonItem.setEndRow(false);
		saveButtonItem.setStartRow(false);

		form.setCellPadding(5);
		form.setFields( textItem, textItem2, textItem3, textItem4, textItem5, new SpacerItem(), new SpacerItem(), cancelButtonItem, saveButtonItem);
		addChild(form);

	}

	@Override
	public DynamicForm getForm() {
		return form;
	}

	@Override
	public String getId() {
		if(id == null){
			return "";
		}else{
			return id;
		}
	}

}
